#encoding=utf-8
'''
Created on 2011-11-14

@author: Sunny

python version:2.6
'''

from corpus import xmlwitch
from corpus.preprocess import untag_sentence


def write_pairs(rte_pairs,fname,verbose='full',with_tag=False):
    '''
            将变换后的rte_pairs存成和rte_labeled_pair一样的格式
            如何存储参照corpus/writer.py

            此处rte_pairs 的类型为RTEPairEx
    '''
    xml = xmlwitch.Builder(version='1.0', encoding='utf-8')
    with xml.dataset(type="mc"):
        for i in range(len(rte_pairs)):
            with xml.pair(id=str(i),label=('Y' if rte_pairs[i].value==1 else 'N')):
                xml.t1(rte_pairs[i].text)
                xml.t2(rte_pairs[i].hyp)
    with open(fname,'wb') as f:
        f.write(str(xml))


def write_pairs_1toN(pair_list,fname,verbose='full',with_tag=False):
    ''' Save the RETPair pair list in xml format
    <entailment_corpus> 
        <TOPIC t_id=pair.topic_id> 
            <H h_id=pair.hyp_id> 
                <H_sentence>pair.hyp</H_sentence> 
            <text doc_id=pair.doc_id s_id=pair.text_id [value=pair.value text=pair.text]/> 
            … 
            </H> 
        </TOPIC> 
    </entailment_corpus>
     
    Keyword arguments:
        pair_list : RTEPairSimple
        fname: xml file name
        verbose: 
            full = include [...]
            positive =only save positive induce
            pure = without [...]                
    '''
    
    pre_hid = pair_list[0].hyp_id
    xml = xmlwitch.Builder(version='1.0', encoding='utf-8')
    with xml.entailment_corpus:
        idx = 0
        while idx<len(pair_list):
            pair = pair_list[idx]
            with xml.TOPIC(t_id=str(pair.topic_id)):
                with xml.H(h_id=pair.hyp_id):
                    if not with_tag:
                        pair.hyp = untag_sentence(pair.hyp)
                    xml.H_sentence(pair.hyp if with_tag else untag_sentence(pair.hyp))
                    
                    while idx < len(pair_list) and pre_hid == pair_list[idx].hyp_id:                        
                        pair = pair_list[idx]
                        if verbose == 'full':
                            xml.text(pair.text if with_tag else untag_sentence(pair.text),
                                     doc_id=str(pair.doc_id),
                                     s_id=str(pair.text_id),
                                     value=('Y' if pair.value==1 else 'N'))
                            
                        elif verbose == 'positive':
                            if pair.value == 1:
                                xml.text(pair.text if with_tag else untag_sentence(pair.text),
                                         doc_id=str(pair.doc_id),
                                         s_id=str(pair.text_id),
                                         value=('Y' if pair.value==1 else 'N'))
                        elif verbose == 'pure':
                            if pair.value == 1:
                                xml.text(None,doc_id=str(pair.doc_id),
                                         s_id=str(pair.text_id))
                        idx += 1
                    if idx < len(pair_list):
                        pre_hid = pair_list[idx].hyp_id

    with open(fname,'wb') as f:
        f.write(str(xml))

if __name__ == '__main__':
    xml = xmlwitch.Builder()
    with xml.a:
        with xml.b:
            temp=5
            xml.c(None,pair=str(temp))
            xml.d('s')
    print(xml)